﻿#region File Description
//-----------------------------------------------------------------------------
// Javier Cantón Ferrero
// javiercantonferrero@gmail.com
// @jcant0n
// http://geeks.ms/blogs/jcanton/
// http://xnacommunity.codeplex.com
//
// MVP XNA/DirectX
//-----------------------------------------------------------------------------
#endregion


#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MyGame.UI;
using MyGame.Interfaces;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input.Touch;
#endregion

namespace MyGame.Screens
{
    public class MainMenuScreen : Screen
    {
        //Fields
        List<IGraphicsControl> controls;

        Texture2D logo;
        Texture2D crowd;

        bool aboutPage;

        #region Properties
        #endregion

        #region Initialize
        public MainMenuScreen()
            : base()
        {
            controls = new List<IGraphicsControl>();

            Vector2 middleScreen = new Vector2(GameGlobals.Width / 2, GameGlobals.Height / 4);
            int offset = 40;

            //play button
            string playText = "Play Game";
            Vector2 playSize = GameGlobals.defaultFont.MeasureString(playText);
            Button play = new Button(playText, GameGlobals.defaultFont, 
                new Vector2(middleScreen.X - playSize.X / 2, middleScreen.Y + offset - playSize.Y / 2));
            play.Color = Color.Black;
            play.Click += new EventHandler(play_Click);

            //exit button
            offset += 60;
            string aboutText = "About";
            Vector2 aboutSize = GameGlobals.defaultFont.MeasureString(aboutText);
            Button about = new Button("About", GameGlobals.defaultFont,
                new Vector2(middleScreen.X - aboutSize.X / 2, middleScreen.Y + offset - aboutSize.Y / 2));
            about.Color = Color.Black;
            about.Click += new EventHandler(delegate { aboutPage = true; });

            //exit button
            offset += 60;
            string exitText = "Exit";
            Vector2 exitSize = GameGlobals.defaultFont.MeasureString(exitText);
            Button exit = new Button("Exit", GameGlobals.defaultFont, 
                new Vector2(middleScreen.X - exitSize.X / 2, middleScreen.Y + offset - exitSize.Y / 2));
            exit.Color = Color.Black;
            exit.Click += new EventHandler(delegate { GameGlobals.isExiting = true; });

            //Add controls
            controls.Add(play);
            controls.Add(about);
            controls.Add(exit);
        }

        public override void LoadContent()
        {
            logo = GameGlobals.content.Load<Texture2D>("logo");
            crowd = GameGlobals.content.Load<Texture2D>("crowd");
        }

        #endregion

        #region Public Methods
        public override void Initialize()
        {

        }

        public override void Update()
        {
            currentState = GamePad.GetState(PlayerIndex.One);

            if (aboutPage)
            {
                if ((currentState.Buttons.Back == ButtonState.Pressed
                && lastState.Buttons.Back == ButtonState.Released) ||
                (TouchPanel.GetState().Count > 0))
                    aboutPage = false;

            }
            else
            {
                if (currentState.Buttons.Back == ButtonState.Pressed
                && lastState.Buttons.Back == ButtonState.Released)
                    GameGlobals.isExiting = true;
                //Update all controls
                foreach (IGraphicsControl c in controls)
                    c.Update();
            }

            lastState = currentState;
        }

        public override void Draw()
        {
            spriteBatch.Begin();

            GameGlobals.DrawCenterTexture(logo, 40, spriteBatch);
            GameGlobals.DrawCenterTexture(crowd, 470, spriteBatch);

            if (aboutPage)
            {
                int offset = GameGlobals.Height / 4 - 20;
                GameGlobals.DrawCenterString("Authors:",offset,spriteBatch,Color.Black);
                offset += 40;
                GameGlobals.DrawCenterString("Roberto Martin", offset, spriteBatch, Color.Black);
                offset += 40;
                GameGlobals.DrawCenterString("Victor Ferrer", offset, spriteBatch, Color.Black);
                offset += 60;
                GameGlobals.DrawCenterString("No bulls were damaged", offset, spriteBatch, Color.Black);
                offset += 40;
                GameGlobals.DrawCenterString("during the developping", offset, spriteBatch, Color.Black);
                offset += 40;
                GameGlobals.DrawCenterString("stage ;)", offset, spriteBatch, Color.Black);
            }
            else
            {
                //Draw all controls
                foreach (IGraphicsControl c in controls)
                    c.Draw(spriteBatch);
            }

            spriteBatch.End();
        }
        #endregion

        #region Private Methods
        private void play_Click(object sender, EventArgs e)
        {
            ScreenManager.TransitionTo("PlayGame", "BlackFade");
        }

        #endregion
    }
}
